// pages/public/village_topic /village_topic.js
import {
    _get,
    _headerGet,
    _headerPost,
    config
} from '../../../utils/httpUtils.js';
const app = getApp();
Page({
    data: {
        navbarData: {
            showCapsule: 1, //是否显示左上角图标
            title: '我为家乡' //导航栏 中间的标题
        },
        height: app.globalData.height * 2 + 20,
        userName: '',
        dynamic: {}, //列表 
        showComment: true, //显示表单
        focus: false //聚焦输入框
    },
    onLoad: function(options) {
        this.setData({
            queryData: JSON.parse(options.queryData)
        })
        if (this.data.queryData.topic_id == 1) {
            this.getDynamicCate(1)
            this.setData({
                navbarData: {
                    'showCapsule': 1,
                    'title': "我为家乡代言"
                }
            })
        } else if (this.data.queryData.topic_id == 2) {
            this.getList(1)
            this.setData({
                navbarData: {
                    'showCapsule': 1,
                    'title': "我为家乡创收"
                }
            })
        }
        let that = this
        wx.getStorage({ // 取出登录缓存的用户昵称
            key: 'nickname',
            success: function(res) {
                that.data.userName = res.data
            },
        })
    },

    getDynamicCate(topic_id) { //获取动态分类
        _get('/api/dynamic/getDynamicCate', {
            'topic_id': topic_id
        }, res => {
            if (res.data.code == 1) {
                this.setData({
                    dynamicCate: res.data.data,
                    cate_id: res.data.data[0].dynamic_cate_id
                })
                this.getdynamic_list(1)
            } else {
                console.log(`api/dynamic/getDynamicCate ERROR`)
            }
        })
    },

    getList(pageNum, pageCount) {
        let _data = {
            'topic_id': this.data.queryData.topic_id,
            'pageCount': pageCount || 10,
            'page': pageNum
        }
        _headerGet('/api/village/i_am_home', _data, res => {
            // console.log(res.data)
            wx.stopPullDownRefresh() //停止下拉动作
            if (res.data.code == 1) {
                if (pageNum == 1) { //下拉重置数据
                    this.setData({
                        dynamic: res.data.data,
                        pageNum: pageNum
                    })
                } else {
                    this.setData({
                        dynamic: this.data.dynamic.concat(res.data.data),
                        pageNum: pageNum
                    })
                }
            } else {
                wx.showToast({
                    title: '没有更多数据了',
                    duration: 2000,
                    icon: 'none'
                })
            }
        })
    },

    getdynamic_list(pageNum, pageCount) {
        let _data = {
            'dynamic_cate_id': this.data.cate_id,
            'pageCount': pageCount || 10,
            'topic_id': this.data.queryData.topic_id,
            'page': pageNum
        }
        _headerGet('/api/dynamic/dynamic_list', _data, res => {
            // console.log(res.data)
            wx.stopPullDownRefresh() //停止下拉动作
            if (res.data.code == 1) {
                if (pageNum == 1) { //下拉重置数据
                    this.setData({
                        dynamic: res.data.data,
                        pageNum: pageNum
                    })
                } else {
                    this.setData({
                        dynamic: this.data.dynamic.concat(res.data.data),
                        pageNum: pageNum
                    })
                }
            } else {
                wx.showToast({
                    title: '没有更多数据了',
                    duration: 2000,
                    icon: 'none'
                })
            }
        })
    },

    previewImage(e) { //图片预览
        let currentSrc = e.currentTarget.dataset.src;
        let currentSrcArray = e.currentTarget.dataset.srcarr;
        wx.previewImage({
            current: currentSrc, // 当前显示图片的http链接
            urls: currentSrcArray // 需要预览的图片http链接列表
        })
    },

    handOver(event) { //切换分类
        let currentId = event.currentTarget.dataset.id;
        this.setData({
            cate_id: currentId,
            dynamic: {},
            pageNum: 1
        })
        if (currentId != 0) {
            this.getdynamic_list(1)
        }
    },

    publish() { //发布帖子页
        let identifier = this.data.queryData.topic_id
        wx.navigateTo({
            url: `/pages/group/publish/publish?identifier=${identifier}`,
        })
    },
    enterDynamicDetail(e) { //进入用户发布详情
        let index = e.currentTarget.dataset.index;
        let _data = this.data.dynamic[index];
        wx.setStorage({
            key: 'userdynamic',
            data: _data,
            success: res => {
                wx.navigateTo({
                    url: '/pages/group/dynamicdetail/dynamicdetail',
                })
            }
        })
    },
    likes(e) { //点赞
        let key = e.currentTarget.id;
        let _isZan = this.data.dynamic[key].dynamic_is_zan;
        let _data = {
            'type': 2,
            'id': e.currentTarget.dataset.id
        }
        if (_isZan == 1) {
            wx.showToast({
                title: '已成功点赞',
                icon: 'none',
                duration: 2000
            })
        } else if (_isZan == 0) {
            this.data.dynamic[key].dynamic_is_zan = 1
            this.data.dynamic[key].dynamic_zan_count++
                this.like_zan(_data)
        }
        this.setData({
            dynamic: this.data.dynamic
        })

    },
    like_zan(data) { //执行点赞
        _headerPost('/api/zan/zan', data, res => {
            if (res.data.code == 1) {
                wx.showToast({
                    title: res.data.msg,
                    icon: 'success',
                    duration: 2000
                })
            } else {
                wx.showToast({
                    title: res.data.msg,
                    icon: 'none',
                    duration: 2000
                })
            }

        })
    },

    outcomment(e) { //调出评论输入框
        this.setData({
            showComment: false,
            focus: true,
            _data: {
                'ac_type': 1,
                'id': e.currentTarget.dataset.acid,
                'key': e.currentTarget.dataset.index
            }
        })
    },
    bindblur() { //评论失焦
        this.setData({
            showComment: true,
            focus: false
        })
    },

    comment(e) { //发布评论
        let data = this.data._data;
        data.content = e.detail.value.content
        // console.log(data)
        let key = data.key
        let comment = {
            content: data.content,
            u_nickname: this.data.userName
        }
        _headerPost('/api/comment/addComment', data, res => {
            // console.log(res.data)
            if (res.data.code == 1) {
                wx.showToast({
                    title: res.data.msg,
                    icon: 'success',
                    duration: 1000
                })
                this.data.dynamic[key].dynamic_comment_list.push(comment) //模拟假数据实时添加
                this.setData({
                    showComment: true,
                    tmpStorage: '', //置空评论框
                    dynamic: this.data.dynamic //数据重载
                })
            } else {
                wx.showToast({
                    title: res.data.msg,
                    duration: 2000,
                    icon: 'none'
                })
            }
        })
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        wx.removeStorage({ //清除userdynamic缓存 
            key: 'userdynamic',
            success: function(res) {
                // console.log('已清除userdynamic缓存')
            },
        })
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {
        if (this.data.queryData.topic_id == 1) {
            this.getdynamic_list(1)
        } else {
            this.getList(1)
        }
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {
        if (this.data.queryData.topic_id == 1) {
            this.getdynamic_list(this.data.pageNum + 1)
        } else {
            this.getList(this.data.pageNum + 1)
        }
    }
})